package com.ruoyi.experiment.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.experiment.mapper.EmergencyPlanMapper;
import com.ruoyi.experiment.domain.EmergencyPlan;
import com.ruoyi.experiment.service.IEmergencyPlanService;

/**
 * 应急预案Service业务层处理
 * 
 * @author gw
 * @date 2025-01-08
 */
@Service
public class EmergencyPlanServiceImpl implements IEmergencyPlanService 
{
    @Autowired
    private EmergencyPlanMapper emergencyPlanMapper;

    /**
     * 查询应急预案
     * 
     * @param planId 应急预案主键
     * @return 应急预案
     */
    @Override
    public EmergencyPlan selectEmergencyPlanByPlanId(Long planId)
    {
        return emergencyPlanMapper.selectEmergencyPlanByPlanId(planId);
    }

    /**
     * 查询应急预案列表
     * 
     * @param emergencyPlan 应急预案
     * @return 应急预案
     */
    @Override
    public List<EmergencyPlan> selectEmergencyPlanList(EmergencyPlan emergencyPlan)
    {
        return emergencyPlanMapper.selectEmergencyPlanList(emergencyPlan);
    }

    /**
     * 新增应急预案
     * 
     * @param emergencyPlan 应急预案
     * @return 结果
     */
    @Override
    public int insertEmergencyPlan(EmergencyPlan emergencyPlan)
    {
        emergencyPlan.setCreateTime(DateUtils.getNowDate());
        emergencyPlan.setCreateBy(SecurityUtils.getUsername());
        return emergencyPlanMapper.insertEmergencyPlan(emergencyPlan);
    }

    /**
     * 修改应急预案
     * 
     * @param emergencyPlan 应急预案
     * @return 结果
     */
    @Override
    public int updateEmergencyPlan(EmergencyPlan emergencyPlan)
    {
        emergencyPlan.setUpdateTime(DateUtils.getNowDate());
        emergencyPlan.setUpdateBy(SecurityUtils.getUsername());
        return emergencyPlanMapper.updateEmergencyPlan(emergencyPlan);
    }

    /**
     * 批量删除应急预案
     * 
     * @param planIds 需要删除的应急预案主键
     * @return 结果
     */
    @Override
    public int deleteEmergencyPlanByPlanIds(Long[] planIds)
    {
        int deletedCount = 0;
        for (Long id : planIds){
            EmergencyPlan emergencyPlan = emergencyPlanMapper.selectEmergencyPlanByPlanId(id);
            emergencyPlan.setDeleteBy(SecurityUtils.getUsername());
            emergencyPlan.setDeleteTime(DateUtils.getNowDate());
            emergencyPlan.setDelFlag(1);
            int result = emergencyPlanMapper.updateEmergencyPlan(emergencyPlan);
            if (result > 0) {
                deletedCount++;
            }
        }
        return deletedCount;
    }

    /**
     * 删除应急预案信息
     * 
     * @param planId 应急预案主键
     * @return 结果
     */
    @Override
    public int deleteEmergencyPlanByPlanId(Long planId)
    {
        EmergencyPlan emergencyPlan = emergencyPlanMapper.selectEmergencyPlanByPlanId(planId);
        emergencyPlan.setDeleteBy(SecurityUtils.getUsername());
        emergencyPlan.setDeleteTime(DateUtils.getNowDate());
        emergencyPlan.setDelFlag(1);
        return emergencyPlanMapper.updateEmergencyPlan(emergencyPlan);
    }
}
